package com.google.coffeeshop.gui.desktop.orderpayment.seatdetail;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.DefaultTableModel;

import vn.bang.utility.LongUtil;

import com.google.coffeeshop.business.OrderBusiness;
import com.google.coffeeshop.entity.OrderLineDTO;
import com.google.coffeeshop.gui.desktop.AbstractBaseTable;

public class SeatTable extends AbstractBaseTable {
	private static final long serialVersionUID = 1L;
	
	private static String COL_OL_ID = "Order line ID";
	private static String COL_PRODUCT_ID = "Product ID";
	private static String COL_OL_NUMBER = "Order Line Number";
	private static String COL_PRODUCT = "Product";
	private static String COL_QUANTITY = "Quantity";
	private static String COL_PRICE_PER_UNIT = "Price per Unit";
	private static String COL_AMOUNT = "Amount";
	
	private static Object[][] rowData = new Object[][] {};
	private static String[] columnNames = new String[] {
		COL_OL_ID, COL_PRODUCT_ID, COL_OL_NUMBER, COL_PRODUCT, COL_QUANTITY, COL_PRICE_PER_UNIT, COL_AMOUNT
		};
	
	private DefaultTableModel model = new DefaultTableModel(rowData, columnNames);
	
	public SeatTable() {
		super(rowData, columnNames);
		setModel(model);
		invisibleColumn(0);
		invisibleColumn(0);
	}
	
	public void removeAllRow() {
		model.setRowCount(0);
	}
	
	private void invisibleColumn(int columnIndex) {
		this.getColumnModel().removeColumn(this.getColumnModel().getColumn(columnIndex));
	}
	
	private void addNewRow(long orderLineId, long productId, String productName, long quantity, long pricePerUnit) {
		Object[] objects = new Object[] {orderLineId, productId, model.getRowCount() + 1, productName, quantity, pricePerUnit, pricePerUnit * quantity};
		model.addRow(objects);
	}

	private void addNewRow(OrderLineDTO orderLine) {
		addNewRow(orderLine.getId(), orderLine.getProduct().getId(), orderLine.getProduct().getName(), orderLine.getQuantity(), orderLine.getPriceOfUnit());
	}
	
	public void addNewRows(List<OrderLineDTO> orderLines) {
		for (OrderLineDTO orderLine : orderLines) {
			addNewRow(orderLine);
		}
		arrageOrderLineNumbers();
	}
	
	public long getSelectedProductId() {
		int rowIndex = this.getSelectedRow();
		return (long)findModelValue(model, COL_OL_ID, rowIndex);
	}
	
	public List<Long> getSelectedProductIds() {
		List<Long> ids = new ArrayList<Long>();
		for (int rowIndex : this.getSelectedRows()) {
			ids.add((long)findModelValue(model, COL_OL_ID, rowIndex));
		}
		return ids;
	}
	
	public List<OrderLineDTO> getOrderLines() {
		List<OrderLineDTO> orderLines = new ArrayList<OrderLineDTO>(); 
		for (int rowIndex = 0; rowIndex < model.getRowCount(); rowIndex++) {
			OrderLineDTO orderLine = new OrderLineDTO();
			orderLine.setId(
					(long)findModelValue(model, COL_OL_ID, rowIndex));
			orderLine.setProduct(
					OrderBusiness.findProduct(LongUtil.valueOf(
							String.valueOf(findModelValue(model, COL_PRODUCT_ID, rowIndex)))));
			orderLine.setQuantity(
					LongUtil.valueOf(
							String.valueOf(findModelValue(model, COL_QUANTITY, rowIndex))));
			orderLine.setPriceOfUnit(
					LongUtil.valueOf(
							String.valueOf(findModelValue(model, COL_PRICE_PER_UNIT, rowIndex))));
			orderLines.add(orderLine);
		}
		return null;
	}

	private void arrageOrderLineNumbers() {
		for (int rowIndex = 0; rowIndex < model.getRowCount(); rowIndex++) {
			model.setValueAt(rowIndex + 1, rowIndex, 1);
		}
	}
	
}
